package com.cjkj.user.service.impl;

import com.cjkj.common.core.domain.AjaxResult;
import com.cjkj.common.utils.DateUtils;
import com.cjkj.common.utils.uuid.IdUtils;
import com.cjkj.equipment.domain.Equipment;
import com.cjkj.equipment.domain.EquipmentUsageRecords;
import com.cjkj.equipment.mapper.EquipmentUsageRecordsMapper;
import com.cjkj.equipment.service.IEquipmentService;
import com.cjkj.user.service.UserEquipmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
public class UserEquipmentServiceImpl implements UserEquipmentService {

    @Resource
    private IEquipmentService equipmentService;
    @Resource
    private EquipmentUsageRecordsMapper equipmentUsageRecordsMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public AjaxResult userSetEquipment(Map<String, Object> para) {
        // 获取参数
        String equipmentNo = (String) para.get("equipmentNo");
        String equipmentType = (String) para.get("equipmentType");
        String phone = (String) para.get("phone");

        // 查询设备
        Equipment equipment = equipmentService.getEquipment(equipmentNo, equipmentType);
        if (equipment == null) {
            return AjaxResult.error("设备不存在！");
        }
        if (equipment.getStatus() == 1) {
            return AjaxResult.error("设备已被使用！");
        }
        // 修改设备状态
        equipment.setStatus(1);
        boolean update = equipmentService.updateEquipment(equipment) > 0;

        // 添加使用记录
        EquipmentUsageRecords usageRecords = new EquipmentUsageRecords();
        usageRecords.setId(IdUtils.fastSimpleUUID());
        usageRecords.setEquipmentNo(equipmentNo);
        usageRecords.setEquipmentType(equipment.getEquipmentType());
        usageRecords.setPhone(phone);
        usageRecords.setStartTime(DateUtils.getNowDate());

        boolean save = equipmentUsageRecordsMapper.insertEquipmentUsageRecords(usageRecords) > 0;
        if (save && update) {
            return AjaxResult.success("绑定成功！");
        } else {
            throw new RuntimeException("绑定失败！");
        }
    }

    @Override
    public AjaxResult unboundEquipment(Map<String, String> para) {

        // 获取参数信息
        String equipmentNo = para.get("equipmentNo");
        String equipmentType = para.get("equipmentType");
        String phone = para.get("phone");

        // 查询设备
        Equipment equipment = equipmentService.getEquipment(equipmentNo, equipmentType);
        if (equipment == null) {
            return AjaxResult.error("设备不存在！");
        }

        // 修改设备状态
        equipment.setStatus(0);
        boolean updateEquipment = equipmentService.updateEquipment(equipment) > 0;

        // 修改使用记录
        EquipmentUsageRecords inUse = equipmentUsageRecordsMapper.getEquipmentInUse(equipmentNo, equipmentType, phone);
        if (inUse != null) {
            inUse.setEndTime(new Date());
            boolean updateRecord = equipmentUsageRecordsMapper.updateEquipmentUsageRecords(inUse) > 0;
            if (updateEquipment && updateRecord) {
                return AjaxResult.success("解绑成功！");
            } else {
                throw new RuntimeException("解绑失败！");
            }
        } else {
            return AjaxResult.error("未找到设备使用记录！");
        }

    }
}
